#include "Player.hpp"

Player::Player() :
    GameObject(),
    m_direction(1),
    m_offsetY(90.f)
{
    m_state = MOVE;
    setDistance(50-10);
    setSpeed(sf::Vector2<float>(0.5,0.1f));
}

Player::~Player() {
}

void Player::update() {
    //move();
    switch ( m_state ) {
        case MOVE:
            move();
            break;
        case JUMP:
            jump();
            break;
        case CHANGE_DIRECTION:
            changeDirection();
            break;
        case FALL:
            fall();
            break;
        case BACK_TO_GROUND:
            backToGround();
            break;
        default:
            break;
    }
}

void Player::move() {
    //sf::Vector2<float> speed(2/(pow(2,7-floor()+5)/360),-0.5);
    sf::Vector2<float> speed(0.5,-0.5);
    setAngle(angle()+(speed.x*direction()));
    //setDistance(distance()+speed().y);
}

void Player::setDistance(float d){
	m_distance = d;
}

void Player::jump() {
    sf::Vector2<float> jumpSpeed(0.5,-1.0);
    setAngle(angle()+(jumpSpeed.x*direction()));
    //setDistance(distance()+jumpSpeed.y);
    //m_offsetY -= -jumpSpeed.y;
    if ( distance() > m_offsetY ) {
        setDistance(distance()+jumpSpeed.y);
    }
    else {
        //m_offsetY = 50;
        setState(BACK_TO_GROUND);
    }
}

void Player::changeDirection() {
    if ( m_previousState == MOVE ) {
        setDirection(direction()*-1);
    }
    setState(m_previousState);
}

void Player::fall() {
    sf::Vector2<float> fallSpeed(0.5,0.5);
    setAngle(angle()+(fallSpeed.x*direction()));
    //m_offsetY += distance()+speed().y;
    m_offsetY -= fallSpeed.y;
    if ( distance() < m_offsetY ) {
        setDistance(distance()+fallSpeed.y);
    }
    else {
        //m_offsetY = 50;
        setState(MOVE);
    }
}

void Player::backToGround() {
    sf::Vector2<float> fallSpeed(0.5,0.5);
    setAngle(angle()+(fallSpeed.x*direction()));
    m_offsetY -= fallSpeed.y;
    if ( distance() < m_offsetY ) {
        setDistance(distance()+fallSpeed.y);
    }
    else {
        setState(MOVE);
    }
}

int Player::direction() const {
    return m_direction;
}

const sf::Vector2<float>& Player::speed() const {
    return m_speed;
}

float Player::offsetY() {
    return m_offsetY;
}

void Player::setDirection(int direction) {
    m_direction = direction;
}

void Player::setSpeed(const sf::Vector2<float>& speed) {
    m_speed = speed;
}

void Player::setOffsetY(float offsetY) {
    m_offsetY = offsetY;
}

void Player::setState(State state) {
    if ( m_state != state ) {
        m_previousState = m_state;
        m_state = state;

        if ( m_state == JUMP ) {
            if ( m_previousState == MOVE ) {
                //setOffsetY(distance()-50);
                setOffsetY(((7-floor()-1)*50)-10-25); // pour sauter un peu plus haut
            }
            else {
                setState(MOVE);
            }
        }

        if ( m_state == FALL ) {
            //setOffsetY(distance()+50);
            setOffsetY(((7-floor()+1)*50)+30);
        }

        if ( m_state == BACK_TO_GROUND ) {
            //setOffsetY(distance()+50);
            setOffsetY(((7-floor()+1)*50)+10);
        }
    }
}
